<template>
  <div class="my-container">
    <div class="header not-login" v-if="!userInfo">
      <div class="login-btn" @click="$router.push('/login')">
        <img class="mobile-img" src="~@/assets/image/mobile.png" />
        <span class="text">登录 / 注册</span>
      </div>
    </div>
    <div class="header userInfo" v-else>
      <!-- 基本信息 -->
      <div class="base">
        <div class="left">
          <van-image fit="cover" :src="userInfos.photo" class="avatar" round />
          <span class="name">{{ userInfos.name }}</span>
        </div>
        <div class="right">
          <van-button
            class="vbtn"
            type="default"
            size="mini"
            round
            to="/user/profile"
            >编辑资料</van-button
          >
        </div>
      </div>
      <!-- 粉丝、关注 -->
      <div class="data">
        <div class="data-item">
          <span>{{ userInfos.art_count }}</span>
          <span>头条</span>
        </div>
        <div class="data-item">
          <span>{{ userInfos.follow_count }}</span>
          <span>关注</span>
        </div>
        <div class="data-item">
          <span>{{ userInfos.fans_count }}</span>
          <span>粉丝</span>
        </div>
        <div class="data-item">
          <span>{{ userInfos.like_count }}</span>
          <span>获赞</span>
        </div>
      </div>

      <!-- 收藏历史模块 -->
      <van-grid clickable :column-num="2" class="nav-grid">
        <van-grid-item to="/">
          <template #icon>
            <i class="iconfont icon-shoucang"></i>
          </template>
          <template #text>
            <span class="text">收藏</span>
          </template>
        </van-grid-item>
        <van-grid-item to="/" badge="99+">
          <template #icon>
            <i class="iconfont icon-lishi"></i>
          </template>
          <template #text>
            <span class="text">历史</span>
          </template>
        </van-grid-item>
      </van-grid>
    </div>
    <van-cell title="消息通知" is-link />
    <van-cell title="小智同学" is-link />
    <van-cell
      class="logout-cell"
      v-if="userInfo"
      title="退出登录"
      @click="onLogout"
      center
    />
  </div>
</template>

<script>
import { getUserInfo } from '@/api/user.js'
export default {
  name: 'MyPage',
  components: {},
  props: {},
  data () {
    return {
      userInfos: {}
    }
  },
  computed: {
    userInfo () {
      return this.$store.state.user
    }
  },
  watch: {},
  created () {
    if (this.userInfo) {
      this.loadUser()
    }
  },
  mounted () {
    // console.log(this.userInfo)
  },
  methods: {
    // 获取用户信息
    async loadUser () {
      try {
        const { data } = await getUserInfo()
        this.userInfos = data.data
      } catch (err) {
        console.log(err)
        this.$toast('获取用户数据信息失败')
      }
    },
    onLogout () {
      this.$dialog
        .confirm({
          title: '黑马头条',
          message: '你真的要退出吗'
        })
        .then(() => {
          this.$store.commit('setUser', null)
        })
        .catch(() => {
          // on cancel
        })
    }
  }
}
</script>

<style lang="less" scoped>
.my-container {
  .header {
    // height: 361px;
    background: url('~@/assets/image/banner.png') no-repeat;
    background-size: cover;
    &.not-login {
      height: 361px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .mobile-img {
      width: 132px;
      height: 132px;
    }
    .login-btn {
      display: flex;
      flex-direction: column;
      align-items: center;
      .text {
        font-size: 28px;
        color: #fff;
        margin-top: 15px;
      }
    }
  }
  .userInfo {
    .base {
      height: 231px;
      padding: 76px 32px 23px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      box-sizing: border-box;
      .left {
        display: flex;
        align-items: center;
        .avatar {
          width: 132px;
          height: 132px;
          border: 3px solid #fff;
        }
        .name {
          font-size: 29px;
          margin-left: 22px;
          color: #fff;
        }
      }
      .right {
        .vbtn {
          padding: 0 20px;
        }
      }
    }
    .data {
      display: flex;
      justify-content: space-around;
      .data-item {
        height: 130px;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: column;
        color: #fff;
        span:nth-child(1) {
          font-size: 36px;
          margin-bottom: 15px;
        }
        span:nth-child(2) {
          font-size: 23px;
        }
      }
    }
  }
  .nav-grid {
    background: #fff;
    .iconfont {
      font-size: 45px;
    }
    .icon-shoucang {
      color: #eb5253;
    }
    .icon-lishi {
      color: #ff9d1d;
    }
    .text {
      color: #000;
      margin-top: 8px;
      font-size: 28px;
    }
  }
  .logout-cell {
    height: 100px;
    text-align: center;
    color: #eb5253;
    margin: 10px 0;
    font-size: 30px;
  }
}
</style>
